package jdklearning.regex;


import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 1. 正则有多行模式和单行模式 eclipse中   (?s) 开头表示多行
 * 
 * @author Administrator
 *
 */
public class RegexPatternMatcher {


	/**
	 * 贪婪模式
	 */
	public static void greedyOperator(){
		String str = "aaaa5bbbb6";

		/*
		 * 贪婪的
		 * 一次吃进最多的10个字符,不匹配,再吐出来一个,匹配了,结束了
		 */
		String regex1 = ".{3,10}[0-9]";
		Matcher m1 = Pattern.compile(regex1).matcher(str);
		if (m1.find())
			System.out.println(m1.start() + "-" + m1.end());
		else
			System.out.println("not match!");

		/*
		 * 非贪婪的
		 * 一次吃进最少的3个字符,然后看后面那个是不是数字,
		 * 再吞一个,然后再看后面那个是不是数字,匹配了,结束了
		 */
		regex1 = ".{3,10}?[0-9]";
		m1 = Pattern.compile(regex1).matcher(str);
		if (m1.find())
			System.out.println(m1.start() + "-" + m1.end());
		else
			System.out.println("not match!");

		/*
		 * 独占的
		 * 一次吃进最多的10个字符,不匹配,不吐出来,不匹配了,结束了
		 */
		regex1 = ".{3,10}+[0-9]";
		m1 = Pattern.compile(regex1).matcher(str);
		if (m1.find())
			System.out.println(m1.start() + "-" + m1.end());
		else
			System.out.println("not match!");
	}

	
	public static void basicTest(){
		//一个反斜线
		String str1 = "\\";
		//正则表达式的一个反斜线使用两个反斜线,在java中表示就再需要两个反斜线转义
		System.out.println(str1.matches("\\\\"));
		//POSIX Style,这种写法不建议使用,如果要使用查API
		System.out.println("a".matches("\\p{Lower}"));
		//空白行
		" \n".matches("^[\\s&&[^\n]]*\\n$");
		//邮箱
		String regex1 = "[\\w[.-]]+";
		String regex2 = "@";
		String regex3 = "[\\w[.-]]+";
		String regex4 = "\\.";
		String regex5 = "[\\w]+";
		String regex = regex1+regex2+regex3+regex4+regex5;
		System.out.println("156112846@qq.com".matches(regex));
	}


	/**
	 * 1.注意转义(正则转义(-,\w,\d等),加上java转义
	 * 2.$获取group内容, "$1"引用第一个group中的内容
	 * 3.作用可以理解为保留group中的内容
	 */
	public static void groupReference(){
		String source2="<span class=\"adsds&bdsdsd\">rewrewrwrw<span class=\"adsds&bdsdsd\">";
		String regex="(<span class=\"[A-Za-z0-9\\-]+)&";
		String result1=source2.replaceAll(regex, "$1 ");
		String result2=source2.replaceAll(regex, "\\$1 ");
		String result3=source2.replace(regex, "\\$1 ");
		System.out.println(result1);
		System.out.println(result2);
		System.out.println(result3);

		System.out.println(
				"aaa111bbb2222ccc"
				.replaceAll("aaa(.*?)bbb(.*?)ccc","***$1***$2***"));
		String result4="i am i am a person"
				.replaceAll("(i )am", "$1am not");
		System.out.println(result4);
	}


	/**
	 * group保留
	 */
	public static void RegexAPI(){
		String source1="one cat two cats in the yard";
		Pattern p1=Pattern.compile("cat");		
		Matcher m1 = p1.matcher(source1);
		StringBuffer sb1 = new StringBuffer();
		while (m1.find()) {
			System.out.println(m1.start(0) + "-" + m1.end(0));
		    m1.appendReplacement(sb1, "dog");
		}
		m1.appendTail(sb1);
		System.out.println(sb1.toString());	
		////////////////////////////////
		Pattern p2=Pattern.compile("(one )cat");
		Matcher m2=p2.matcher(source1);
		StringBuffer sb2 = new StringBuffer();
		while(m2.find()){
			System.out.println(m2.start(0)+"-"+m2.end(0));
			System.out.println(m2.start(1)+"-"+m2.end(1));
			m2.appendReplacement(sb2, "$1dog");
			System.out.println(sb2.toString());
		}
		m2.appendTail(sb2);
		System.out.println(sb2);
	}
	
	public static void main(String[] args) {
		groupReference();
//		RegexAPI();
//		greedyOperator();
	}
}